package com.gaoyi.crm.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.crm.R;
import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.apiresult.PagedList;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.db.bean.ResultBean;
import com.gaoyi.crm.db.dto.PmnDto;
import com.gaoyi.crm.db.query.MtcSyncedQuery;
import com.gaoyi.crm.repository.PmnRepository;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.vm.base.BaseListViewModel;

import java.util.List;

public class MtcSyncedListViewModel extends BaseListViewModel<PmnDto> {

    MutableLiveData<Integer> mTotal;
    MutableLiveData<Integer> mDeleteCode;
    PmnRepository mRepository;
    MutableLiveData<ResultBean> mResult;

    public MutableLiveData<ResultBean> getResult() {
        if (mResult == null) {
            mResult = new MutableLiveData<>();
        }
        return mResult;
    }

    public void setResult(ResultBean result) {
        mResult.setValue(result);
    }

    public MutableLiveData<Integer> getTotal() {
        if (mTotal == null) {
            mTotal = new MutableLiveData<>();
        }
        return mTotal;
    }

    public MutableLiveData<Integer> getDeleteCode() {
        if (mDeleteCode == null) {
            mDeleteCode = new MutableLiveData<>();
        }
        return mDeleteCode;
    }

    public MtcSyncedListViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PmnRepository();
    }

    public void getSyncedPmn(MtcSyncedQuery query) {
        mRepository.getPmnBySyncedAt(query,new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<PagedList<PmnDto>> apiResultData = (ApiResultData<PagedList<PmnDto>>) apiResult;
                mResultCode.setValue(apiResultData.getStatus());
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                    return;
                }
                PagedList<PmnDto> data = apiResultData.getData();
                if (data != null) {
                    mTotal.setValue(data.getTotalCount());
                    List<PmnDto> list = data.getList();
                    if (list != null && !list.isEmpty()) {
                        addListValue(list);
                    }
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                mResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        });
    }

    public void delete(List<String> list){
        if (getView() != null) {
            getView().showLoading();
        }
        mRepository.deletePmn(list, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<String> apiResultData = (ApiResultData<String>) apiResult;
//                ToastUtil.showToast(apiResultData.getMessage());
                if (apiResultData.getStatus() == ApiStatus.SUCCESS) {
                    mDeleteCode.setValue(ApiStatus.SUCCESS);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ToastUtil.showToast(R.string.network_error);
            }
        });
    }
}
